| Conditions | 1 |
| Paths | 1 |
| Total Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 9 | const Database = () => { |
||
| 10 | const __ = { |
||
| 11 | getDatabase: database => { |
||
| 12 | if (!fs.existsSync(database)) { |
||
| 13 | return {} |
||
| 14 | } |
||
| 15 | |||
| 16 | return JSON.parse(fs.readFileSync(database, 'utf8')) |
||
| 17 | } |
||
| 18 | } |
||
| 19 | |||
| 20 | const proto = { |
||
| 21 | data: '', |
||
| 22 | database: '', |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set file database. |
||
| 26 | * |
||
| 27 | * @param {string} pathname |
||
| 28 | */ |
||
| 29 | setFile: function (pathname) { |
||
| 30 | let regex = new RegExp(/\.json$/) |
||
| 31 | |||
| 32 | if (!regex.test(pathname)) { |
||
| 33 | throw new Error('The database file must be a json file') |
||
| 34 | } |
||
| 35 | |||
| 36 | this.database = pathname |
||
| 37 | |||
| 38 | return this |
||
| 39 | }, |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add a key and value for database. |
||
| 43 | * |
||
| 44 | * @param {string} key |
||
| 45 | * @param {string} value |
||
| 46 | */ |
||
| 47 | add: function (key, value) { |
||
| 48 | this.data = __.getDatabase(this.database) |
||
| 49 | this.data[key] = value |
||
| 50 | |||
| 51 | return this |
||
| 52 | }, |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add values to env vars from node. |
||
| 56 | * |
||
| 57 | * @param {object} data |
||
| 58 | */ |
||
| 59 | addEnv: function (data) { |
||
| 60 | if (typeof data !== 'object') { |
||
| 61 | throw new Error('data must be an object') |
||
| 62 | } |
||
| 63 | |||
| 64 | _.map(data, (value, key) => { |
||
| 65 | process.env[key] = value |
||
| 66 | }) |
||
| 67 | }, |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Add multiples keys to database. |
||
| 71 | * |
||
| 72 | * @param {object} object |
||
|
|
|||
| 73 | */ |
||
| 74 | massive: function (data) { |
||
| 75 | if (typeof data !== 'object') { |
||
| 76 | throw new Error('data must be an object') |
||
| 77 | } |
||
| 78 | |||
| 79 | _.map(data, (value, key) => this.add(key, value)) |
||
| 80 | |||
| 81 | return this |
||
| 82 | }, |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return an key from database or all data. |
||
| 86 | * |
||
| 87 | * @param {string} item |
||
| 88 | */ |
||
| 89 | get: function (item) { |
||
| 90 | let db = __.getDatabase(this.database) |
||
| 91 | |||
| 92 | if (item !== undefined && !(item in db)) { |
||
| 93 | throw new Error(`${item} is undefined`) |
||
| 94 | } |
||
| 95 | |||
| 96 | return db[item] || db |
||
| 97 | }, |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Store data to db.json. |
||
| 101 | */ |
||
| 102 | store: function () { |
||
| 103 | fs.writeFile(this.database, JSON.stringify(this.data), err => { |
||
| 104 | if (err) { |
||
| 105 | throw new Error(err) |
||
| 106 | } |
||
| 107 | |||
| 108 | return |
||
| 109 | }) |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return Object.create(proto) |
||
| 114 | } |
||
| 115 | |||
| 117 |